	var map = null;
	var name = "me";
	var uid = 0;
	var level = 8;
	var prefix = "www";
	var myfriends = new Array();
	
	var geo = null;
	var timer = self;
	var isGear = google != null && google.gears != null;
	var isHtml5 = navigator != null && navigator.geolocation != null;
	var isLocalStorage = localStorage != null;
	
	var myMarker = null;
	var currentLocation = null;
	
	var markers = new Array();
	
	var infowindow = new google.maps.InfoWindow();
	
	if(isGear) {
		timer = google.gears.factory.create('beta.timer');
		geo = google.gears.factory.create('beta.geolocation');
	}
	
	if(isHtml5) {
		isGear = false;
		timer = window;
		geo = navigator.geolocation;
	}
	
	function getRequest() {
		return new XMLHttpRequest();
	}
	
	function addPoint(marker, name, uid, update) {
		var myHtml = "<strong>";
		myHtml += "<a href=http://" + prefix + ".facebook.com/profile.php?id=";
		myHtml += uid;
		myHtml += " target=_parent>Watch the profile of " + name;
		myHtml += "</a></strong>";
		myHtml += "<p><a href=http://" + prefix + ".facebook.com/addfriend.php?id=";
		myHtml += uid;
		myHtml += " target=_parent>Add as friend</a></p>";
		myHtml += "<p><a href=http://" + prefix + ".facebook.com/inbox/?compose&id=";
		myHtml += uid;
		myHtml += " target=_parent>Send a message</a></p>";
		myHtml += "<p>" + update + "</p>";
		
		google.maps.event.addListener(marker, "click", function() {
			infowindow.setContent(myHtml);
			infowindow.open(map,marker);
		});
	}
	
	function clearAll() {
		while(markers.length > 0) {
			var marker = markers.pop();
			google.maps.event.clearInstanceListeners(marker);
			marker.setMap(null); // remove
		}
	}
	
	function updateMyPosition(position) {
	  var coords = position.coords;
	  var p = new google.maps.LatLng(coords.latitude, coords.longitude);
	  map.setCenter(p);
	  map.setZoom(level);
	  var now = new Date();
	  myMarker.setPosition(p);
	  addPoint(myMarker, "you", uid, now.toLocaleString());
	  update(
	  	coords.latitude, 
	  	coords.longitude, 
	  	name, uid, false,
	  	handleInfo, handleInfo);
	  	
	  currentLocation = coords;
	}
	
	function handleError(positionError) {
	  alert('Attempt to get location failed: ' + positionError.message);
	}

	function handleInfo(message) {
		document.getElementById("info").style.display = "block";
		document.getElementById("info").innerHTML = message;
		timer.setTimeout(function() { document.getElementById("info").style.display = "none"; }, 3000);
	}

    function initialize() {
    	var latlng = new google.maps.LatLng(0, 0);
        var myOptions = {
          zoom: 3,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        myMarker = new google.maps.Marker({position: latlng, map: map}); 
    }

	function populate(url) {
		
		getPeople(
			url,
			function(person) {
				if(person.uid != uid) {
					var point = new google.maps.LatLng(person.latitude,person.longitude);
					var online = person.online == 1;

					var marker = new google.maps.Marker({position: point, map: map});
					if(online) {marker.setIcon('red.png');} 
					else {marker.setIcon('blue.png');}
					
				    addPoint(marker, person.name, person.uid, person.update);
					markers.push(marker);
					handleInfo(person.name + " found =)");
				}
			}, 
			handleInfo);
	    
	}
	
	function showTime() {
		if(geo != null) {
			geo.watchPosition(updateMyPosition, handleError, {maximumAge:0});
		} else {
			alert("You must install Google Gears or use HTML5 browser to update your current location");
		}
	}
	
	function unregister() {
		remove(uid, handleInfo, handleInfo);
		window.location.href="/index.html";
	}
	
	function share(subject, body) {
		if(currentLocation == null) {
			alert("current location is unknown, please try later.");
			return;
		}
		
		
		window.location.href=
			"mailto:?subject=Où? allows you to find your friends" +
			"&body=http://ou-android.appspot.com/static.jsp?pos=" +
			currentLocation.latitude + "," + currentLocation.longitude;
	}
	
	function selectFriends(type) {
		
		clearAll();
		
		switch (type) {
		default:
		case "nearme":
			if(currentLocation == null) {
				alert("current location is unknown, please try later.");
			} else {
				populate('/rest/uids?kind=nearme&latitude=' + currentLocation.latitude + '&longitude=' + currentLocation.longitude);
			}
			break;
		case "all":
			populate('/rest/uids?kind=all');
			break;
		case "online":
			populate('/rest/uids?kind=online');
			break;
		case "myfriends":
			for(var i=0;i<myfriends.length;i++) {
				getPerson(
					myfriends[i], 
					function(person) {
						if(person != null) {
							var point = new google.maps.LatLng(person.latitude,person.longitude);
							var online = person.online == 1;
	
							var marker = new google.maps.Marker({position: point, map: map});
							if(online) {marker.setIcon('red.png');} 
							else {marker.setIcon('blue.png');}
							
						    addPoint(marker, person.name, person.uid, person.update);
							markers.push(marker);
							handleInfo(person.name + " found =)");
						}
					},
					handleInfo);
			}
			break;
		}
	}
